<?php

namespace common\service;


use common\components\HttpClient;

class LogisticsService extends BaseService
{

    /**
     * 获取订单物流信息
     * @param $shippercode string 物流公司编号
     * @param $logisticcode string 物流单号
     * @return bool|mixed
     */
    public static function getOrderTracesByJson($shippercode,$logisticcode){
        if($shippercode == 'OTHER'){
            return json_encode(['Traces'=>'']);
        }
        $requestData= "{'OrderCode':'','ShipperCode':'$shippercode','LogisticCode':'$logisticcode'}";

        $datas = array(
            'EBusinessID' => \Yii::$app->params['logistics']['id'],
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData) ,
            'DataType' => '2',
        );
        $datas['DataSign'] = self::_encrypt($requestData, \Yii::$app->params['logistics']['key']);
        $url = \Yii::$app->params['logistics']['url'];
        $result = HttpClient::post($url,$datas);
        return $result;
    }

    /**
     * 生成签名
     * @param $data
     * @param $appkey
     * @return string
     */
    private static function _encrypt($data, $appkey) {
        return urlencode(base64_encode(md5($data.$appkey)));
    }


}